package com.hengxinguotong.zhihuichengjian.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.maps.model.LatLng;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.MapBuildingBean;
import com.hengxinguotong.zhihuichengjian.ui.HomeActivity;
import com.hengxinguotong.zhihuichengjian.utils.SPUtil;
import com.hengxinguotong.zhihuichengjian.utils.UIHelper;
import com.hengxinguotong.zhihuichengjian.utils.map.NavigationUtils;

/**
 * Created by siact on 2017/8/7.
 */

public class CustomOperativeDialog<T extends MapBuildingBean> extends Dialog implements View.OnClickListener {

    /**展示图片*/
    private ImageView iv_operative_img;
    /**关闭按钮*/
    private ImageView iv_close;
    private Context context;
    /**活动展示图片*/
    private String img_url;
    private TextView mLead;
    private TextView mName;
    private TextView mDesc;
    private TextView mNav;
    private TextView mAccess;
    private LatLng mLatLng;

    public LatLng getLatLng() {
        return mLatLng;
    }

    public void setLatLng(LatLng latLng) {
        mLatLng = latLng;
    }

    private T mData;


    public T getData() {
        return mData;
    }

    public void setData(T data) {
        mData = data;
    }

    public CustomOperativeDialog(Context context) {
        super(context, R.style.ActionSheetDialogStyle);
        this.context=context;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_custome_oprative_view);
        iv_operative_img = (ImageView) this.findViewById(R.id.iv_oprative_img);

        mLead = ((TextView) this.findViewById(R.id.txt_lead));
        mName = ((TextView) this.findViewById(R.id.txt_name));
        mDesc = ((TextView) this.findViewById(R.id.txt_desc));
        mName = ((TextView) this.findViewById(R.id.txt_name));
        mNav = ((TextView) this.findViewById(R.id.txt_nav));
        mAccess = ((TextView) this.findViewById(R.id.txt_access));

        iv_close= (ImageView) this.findViewById(R.id.iv_close);


        mNav.setOnClickListener(this);
        mAccess.setOnClickListener(this);
        iv_close.setOnClickListener(this);
    }

    /**设置活动展示图片*/
    public CustomOperativeDialog setOprativeImg(String img_url){
        this.img_url=img_url;
        return this;
    }


    /**dialog显示设置*/
    public void showOprativeDialog(){
        show();
        mLead.setText(mData.getName());
        mName.setText(mData.getAreaName());
        mDesc.setText(mData.getBuilding());

        RequestOptions options = new RequestOptions().dontAnimate().centerCrop().placeholder(R.mipmap.dialog_default_icon);
        Glide.with(context)
                .load(img_url)
                .apply(options)
                .into(iv_operative_img);

        //获取当前Activity所在的窗体
        Window dialogWindow = this.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        dialogWindow.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.y = 0;//设置Dialog距离底部的距离
        // 将属性设置给窗体
        lp.dimAmount = 0.8f;
        lp.width = (int) (UIHelper.getScreenWidth((Activity) context)*0.8);
        dialogWindow.setAttributes(lp);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_close:
                dismiss();
                break;
            case R.id.txt_nav:
                NavigationUtils.Navigation(context,mLatLng);
                break;
            case R.id.txt_access:
                SPUtil.put(context, "enterpriseName", mData.getName());
                SPUtil.put(context, "constructionid", mData.getId());
                Intent intent = new Intent(context, HomeActivity.class);
                context.startActivity(intent);
                dismiss();
                break;
            default:
                break;
        }
    }

}
